<?php
// 开启会话
session_start();

// 初始化游戏数据
if (!isset($_SESSION['game'])) {
    $_SESSION['game'] = [
        'money' => 100, // 初始金钱
        'health' => 100, // 初始生命值
        'heroes' => [], // 玩家拥有的英雄
        'shop' => [
            ['name' => '战士 A', 'cost' => 10, 'power' => 20],
            ['name' => '法师 B', 'cost' => 15, 'power' => 30],
            ['name' => '刺客 C', 'cost' => 20, 'power' => 40]
        ]
    ];
}

// 处理英雄购买操作
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['buy_hero'])) {
    $hero_index = $_POST['hero_index'];
    $hero = $_SESSION['game']['shop'][$hero_index];
    if ($_SESSION['game']['money'] >= $hero['cost']) {
        $_SESSION['game']['money'] -= $hero['cost'];
        $_SESSION['game']['heroes'][] = $hero;
        $message = "成功购买 {$hero['name']}，花费 {$hero['cost']} 金钱。";
    } else {
        $message = "金钱不足，无法购买 {$hero['name']}。";
    }
}

// 处理开始战斗操作
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['start_battle'])) {
    $player_power = 0;
    foreach ($_SESSION['game']['heroes'] as $hero) {
        $player_power += $hero['power'];
    }

    // 简单模拟敌方阵容
    $enemy_power = rand(10, 50);

    if ($player_power > $enemy_power) {
        $message = "战斗胜利！你击败了敌人。";
    } elseif ($player_power < $enemy_power) {
        $_SESSION['game']['health'] -= ($enemy_power - $player_power);
        if ($_SESSION['game']['health'] < 0) {
            $_SESSION['game']['health'] = 0;
        }
        $message = "战斗失败！你的生命值减少至 {$_SESSION['game']['health']}。";
        if ($_SESSION['game']['health'] == 0) {
            $message .= " 游戏结束，你输了。";
            // 重置游戏
            session_destroy();
            session_start();
            $_SESSION['game'] = null;
        }
    } else {
        $message = "战斗平局！双方实力相当。";
    }
}
?>

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>自走棋网页游戏</title>
    <style>
        body {
            font-family: "Microsoft YaHei", sans-serif;
            background-color: #f4f4f4;
            padding: 20px;
        }

        h1 {
            text-align: center;
            color: #333;
        }

        p {
            margin-bottom: 10px;
        }

        form {
            background-color: #fff;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
            margin-bottom: 20px;
        }

        label {
            display: block;
            margin-bottom: 5px;
        }

        select {
            width: 100%;
            padding: 10px;
            margin-bottom: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }

        button {
            padding: 10px 20px;
            background-color: #007BFF;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }

        button:hover {
            background-color: #0056b3;
        }

        .message {
            padding: 10px;
            margin-bottom: 10px;
            border-radius: 5px;
        }

        .success {
            background-color: #d4edda;
            color: #155724;
        }

        .error {
            background-color: #f8d7da;
            color: #721c24;
        }
    </style>
</head>

<body>
    <h1>自走棋网页游戏</h1>
    <?php if (isset($_SESSION['game'])): ?>
        <p>当前金钱: <?php echo $_SESSION['game']['money']; ?></p>
        <p>当前生命值: <?php echo $_SESSION['game']['health']; ?></p>
        <p>拥有的英雄: 
            <?php if (empty($_SESSION['game']['heroes'])): ?>
                暂无
            <?php else: ?>
                <?php foreach ($_SESSION['game']['heroes'] as $hero): ?>
                    <?php echo $hero['name'];?>, 
                <?php endforeach; ?>
            <?php endif; ?>
        </p>

        <?php if (isset($message)): ?>
            <div class="message <?php echo strpos($message, '成功')!== false || strpos($message, '胜利')!== false? 'success' : 'error'; ?>">
                <?php echo $message; ?>
            </div>
        <?php endif; ?>

        <form method="post">
            <label for="hero_index">选择要购买的英雄：</label>
            <select id="hero_index" name="hero_index">
                <?php foreach ($_SESSION['game']['shop'] as $index => $hero): ?>
                    <option value="<?php echo $index; ?>">
                        <?php echo $hero['name'];?>（价格: <?php echo $hero['cost'];?>，战斗力: <?php echo $hero['power'];?>）
                    </option>
                <?php endforeach; ?>
            </select>
            <button type="submit" name="buy_hero">购买英雄</button>
        </form>

        <form method="post">
            <button type="submit" name="start_battle">开始战斗</button>
        </form>
    <?php else: ?>
        <p>游戏结束，你可以刷新页面重新开始。</p>
    <?php endif; ?>
</body>

</html>